/*
 * @Author: abdusemet
 * @Date: 2022-04-11 13:57:18
 * @LastEditors: abdusemet
 * @LastEditTime: 2022-12-24 12:26:02
 * @FilePath: /xjguoyu-web-master/src/utils/utils.ts
 * @Description:
 * 2777208813@qq.com
 * Copyright (c) 2022 by abdusemet, All Rights Reserved.
 */

import tips from "@/utils/tips";
import {PaperType} from "@/types/mutations";

export const formatMoney = (num: any) => {
    if (num == undefined || num == '' || num == null) {
        return "0.00";
    }
    return num.toFixed(2).replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,')
}
//反格式化货币金额
export const unFormatMoney = (num: any) => {
    if (num == undefined || num == '' || num == null) {
        return "0.00";
    }
    return num.toString().replace(/[,]/g, '')
}
// 获取支持 什么纸
export const getCanPaper = (arr: PaperType[], value: PaperType): boolean => {
    if (arr) return arr.includes(value);
    return false;
}

// 获取格式化文件后缀
export const getFileExtension = (filename: string) => {
    return filename.split('.').pop();
}


// 照片编辑 (微信小程序)
export const editImageChange = (src: string): Promise<string> => {
    return new Promise((resolve, reject) => {
        uni.editImage({
            src,
            success: ({tempFilePath}) => resolve(tempFilePath),
            fail: (err) => reject(err)
        });
    });
}

// 获取价格店后面的两位数
export const getPrice = (amount: string) => {
    const parsedAmount = parseFloat(amount);
    if (isNaN(parsedAmount)) {
        return "0.00"; // 或者返回其他默认值
    }
    return parsedAmount.toFixed(2);
}


export const getTypeImage = (ext: string) => {
    ext = ext.includes('.') ? ext.split('.').pop()?.toLowerCase() : ext.toLowerCase();

    const typeMap = {
        'doc': 'word',
        'docx': 'word',
        'word': 'word',
        'pdf': 'pdf',
        'ppt': 'ppt',
        'ppx': 'ppt',
        'pptx': 'ppt',
        'xls': 'xlsx',
        'xlsx': 'xlsx',
        'excel': 'xlsx',
        'png': 'image',
        'jpg': 'image',
        'jpeg': 'image',
        'gif': 'image',
        'bmp': 'image',
        'tif': 'image',
        'tiff': 'image'
    };
    return typeMap[ext] || 'unknown'; // 返回'unknown'处理未知扩展名
}


export const isMoreThanOneDay = (dateString: string): boolean => {
    // بېرىلگەن ۋاقىتنى Date ئوبيېكتىغا ئايلاندۇرۇش
    const givenDate = new Date(dateString);

    // ھازىرقى ۋاقىتنى ئېلىش
    const currentDate = new Date();

    // ۋاقىت پەرقىنى ھېسابلاش (مىللىسېكۇنت)
    const diffInMs = currentDate.getTime() - givenDate.getTime();

    // بىر كۈنلۈك مىللىسېكۇنت
    const oneDayInMs = 24 * 60 * 60 * 1000;

    // بىر كۈندىن كۆپمۇ تەكشۈرۈش
    return diffInMs > oneDayInMs;
}

export const convertToCSTAndFormat = (utcString: string) => {
    // 创建 Date 对象
    const date = new Date(utcString);

    // 设置时区偏移量（东八区）
    const offset = 8 * 60 * 60 * 1000; // 8小时 * 60分钟 * 60秒 * 1000毫秒

    // 转换为东八区时间
    const cstDate = new Date(date.getTime() + offset);

    // 格式化日期
    const year = cstDate.getFullYear();
    const month = String(cstDate.getMonth() + 1).padStart(2, '0');
    const day = String(cstDate.getDate()).padStart(2, '0');
    const hours = String(cstDate.getHours()).padStart(2, '0');
    const minutes = String(cstDate.getMinutes()).padStart(2, '0');
    const seconds = String(cstDate.getSeconds()).padStart(2, '0');

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

// 限制文件大小
export const limitFileSize = (tempSize: number, limitMaxSize: number, text: string) => {
    const maxSize = (limitMaxSize * 1024 * 1024); // 转换为字节
    if (tempSize > maxSize) {
        tips.toast(text);
        return false; // 返回 false 表示文件超出限制
    }
    return true; // 返回 true 表示文件大小合适
}

export const myReplace = (str: string, value: any, reValue: any) => {
    return str.replace(value, reValue);
}

export const copyFun = (data: string) => {
    uni.setClipboardData({
        data,
        success: () => tips.toast('复制成功')
    });
}

export const getFileName = (url: string) => {
    return url.split('/').pop();
}